package com.lexian.service.impl;


import com.lexian.entity.Goodscollectio;
import com.lexian.mapper.GoodscollectioMapper;
import com.lexian.service.GoodscollectioService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lexian.entity.Goodscollectio;
import com.lexian.entity.Goodsstore;
import com.lexian.mapper.GoodscollectioMapper;
import com.lexian.service.GoodscollectioService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lexian.service.GoodsstoreService;
import com.lexian.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;


/**
 * <p>
 * 商品收藏表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-01-19
 */
@Service
public class GoodscollectioServiceImpl extends ServiceImpl<GoodscollectioMapper, Goodscollectio> implements GoodscollectioService {


    //注入门店表业务层对象通过门店持久层操作数据库中的门店表
    @Autowired
    private GoodsstoreService goodsstoreService;


    //添加收藏
    @Override
    public void saveCollectic(Long gid, HttpServletRequest request) {
       Goodscollectio goodscollectio = new Goodscollectio();
        //存入商品id
        goodscollectio.setGoodsId(gid);
        //获取顾客编号
//        String userId = JwtUtils.getMemberIdByJwtToken(request);
//        if(userId!=null){
//            goodscollectio.setCusId(Long.parseLong(userId));
//        }
        //模拟顾客编号
        goodscollectio.setCusId(2L);
        //设置收藏时间（当前）
        goodscollectio.setCollecttime(new Date());
        //根据商品id查询商品所属门店
        //设置条件
        QueryWrapper<Goodsstore> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_id",gid);
        Goodsstore goodsstore = goodsstoreService.getOne(wrapper);
        //设置门店编号
        if(goodsstore!=null && goodsstore.getStoreId()!=null){
            goodscollectio.setStoreNo(goodsstore.getStoreId());
        }

        //添加收藏
        //baseMapper:持久层代理对象
        baseMapper.insert(goodscollectio);
    }

}
